package com.tal.pad.javabase.spider;

import java.nio.file.Path;

public class SpiderMain {

	public static void main(String[] args) throws Exception {
		if (args.length == 0 || hasHelp(args)) {
			printUsage();
			return;
		}
		String url = argOr(args, 0, null);
		String user = argOr(args, 1, "tpp-guest");
		String pass = argOr(args, 2, "##TPPGUEST");
		int depth = Integer.parseInt(argOr(args, 3, "2"));
		String outDir = argOr(args, 4, "downloads");
		long delayMs = Long.parseLong(argOr(args, 5, "200"));
		String mode = argOr(args, 6, url != null && url.contains("/repository/") ? "rest" : "html");
		String maxFilesArg = argOr(args, 7, "-1");
		String includeArg = argOr(args, 8, "jar,sources,javadoc,pom,aar");
		String perLibCapArg = argOr(args, 9, "-1");
		String concurrencyArg = argOr(args, 10, "4");

		RepoSpider spider = new RepoSpider(url, user, pass, depth, delayMs);

		// includes
		spider.setIncludeJar(includeArg.contains("jar"))
				.setIncludeSources(includeArg.contains("sources"))
				.setIncludeJavadoc(includeArg.contains("javadoc"))
				.setIncludeAar(includeArg.contains("aar"))
				.setIncludePom(includeArg.contains("pom"));

		// max files
		long maxFiles = -1L;
		try { maxFiles = Long.parseLong(maxFilesArg); } catch (NumberFormatException ignored) {}
		if (maxFiles > 0) {
			spider.setMaxFiles(maxFiles);
		}
		// per-library cap
		try { spider.setPerLibraryMaxVersions(Integer.parseInt(perLibCapArg)); } catch (NumberFormatException ignored) {}
		// concurrency
		try { spider.setConcurrency(Integer.parseInt(concurrencyArg)); } catch (NumberFormatException ignored) {}

		if ("rest".equalsIgnoreCase(mode)) {
			String repo = parseRepoFromUrl(url);
			if (repo == null) {
				System.out.println("Cannot infer repository name from URL. Provide mode args with explicit repository.");
				printUsage();
				return;
			}
			spider.crawlRest(Path.of(outDir), repo);
		} else {
			spider.crawlTo(Path.of(outDir));
		}
	}

	private static String parseRepoFromUrl(String url) {
		if (url == null) return null;
		int idx = url.indexOf("/repository/");
		if (idx < 0) return null;
		String rest = url.substring(idx + "/repository/".length());
		int slash = rest.indexOf('/');
		return slash > 0 ? rest.substring(0, slash) : rest;
	}

	private static String argOr(String[] args, int idx, String def) {
		return args.length > idx ? args[idx] : def;
	}

	private static boolean hasHelp(String[] args) {
		for (String a : args) {
			if ("-h".equals(a) || "--help".equals(a)) return true;
		}
		return false;
	}

	private static void printUsage() {
		System.out.println("Usage: java SpiderMain <url> [user] [pass] [maxDepth] [downloadDir] [delayMs] [mode(html|rest)] [maxFiles] [includeCsv] [perLibraryVersionCap] [concurrency]");
		System.out.println("includeCsv supports any of: jar,sources,javadoc,pom,aar");
		System.out.println("Examples:");
		System.out.println("  java SpiderMain http://host:8081/repository/maven-public/ tpp-guest ##TPPGUEST 1 ./downloads 100 rest 100 jar,aar 2 6");
		System.out.println("  java SpiderMain http://host/browse/  - - 2 ./downloads 100 html");
	}
} 